package com.tingyuge.base.structrue.set;


import java.util.LinkedList;
import java.util.Queue;
import java.util.Stack;

/**
 * @Created by xiaodao
 */
public class BST<E extends Comparable<E>> {

    private class Node {
        private E e;
        private Node left;
        private Node right;

        public Node(E e) {
            this.e = e;
        }
    }


    private Node root;
    private int size;

    public BST() {
    }

    public int getSize() {
        return size;
    }

    public boolean isEmpty() {
        return size == 0;
    }

   /* public void add(E e ){
        if(root!=null){
           add(root,e);
        }else{
            root = new Node(e);
            size++;

        }
    }
    //以node为根的二分搜索树插入元素 ..递归
    private void add(Node node,E e ){
        if(node.e.equals(e)){
            return;
        }
        if(node.e.compareTo(e)>0){
            //right
            if(node.right == null){
                node.right = new Node(e);
                size++;
                return;
            }
            node= node.right ;
            add(node,e);
        }else{
            if(node.left == null){
                node.left = new Node(e);
                size++;
                return;
            }
            node= node.left ;
            add(node,e);
        }
    }*/

    public void add(E e) {
        root = add(root, e);
    }

    //优化后的添加
    private Node add(Node node, E e) {

        if (node == null) {
            size++;
            return new Node(e);
        }
        if (e.compareTo(node.e) > 0) {
            node.right = add(node.right, e);
        } else if (e.compareTo(node.e) < 0) {
            node.left = add(node.left, e);
        }
        return node;

    }

    /***
     * 查询是否包含
     * @param e
     * @return
     */
    public boolean contains(E e) {
        return contains(root,e);

    }

    private boolean contains(Node node,E e ){

        if(node == null){
            return false;
        }
        if(e.compareTo(node.e) == 0){
            return true;
        }else if(e.compareTo(node.e) > 0 ){
            return contains(node.right,e);
        }else {
            return contains(node.left,e);
        }
    }


    //前序遍历->就是先遍历头节点
    public void preOrder(){
        preOrder(root);
    }

    private void preOrder(Node node){
        if(node == null){
            return;
        }
        System.out.print(node.e);
        preOrder(node.left);
        preOrder(node.right);
    }


    //中序遍历

    public void inOrder(){
        inOrder(root);

    }

    /**
     * 中序遍历,先遍历左子树,然后再遍历右子树 是有序的
     * @param node
     */
    private void inOrder(Node node){

        if(node == null){
            return;
        }
        inOrder(node.left);
        System.out.println(node.e);
        inOrder(node.right);
    }

    /**
     * 先遍历左子树,然后遍历右子树节点,然后再输出本身
     */
    public void afterOrder(){
        afterOrder(root)
        ;
    }

    private void afterOrder(Node node) {
        if(node ==null ){
            return;
        }
        afterOrder(node.left);

        afterOrder(node.right);
        System.out.println(node.e);

    }

    //广度遍历

    public void levelOrder(){
        Queue<Node> q = new LinkedList<>();
        q.add(root);
        while (!q.isEmpty()){
            Node cur = q.remove();
            System.out.println(cur.e);
            if(cur.left != null){
                q.add(cur.left);
            }
            if(cur.right != null){
                q.add(cur.right);
            }
        }
    }

    /**
     * 非递归
     */
    public void preOrderNr(){
        Stack<Node> stack = new Stack<>();
        stack.push(root);
        while (!stack.isEmpty()){
            Node cur =  stack.pop();
            System.out.print(cur.e);
            if(cur.right !=null)
                stack.push(cur.right);
            if(cur.left !=null)
                stack.push(cur.left);

        }
    }

    /**
     * 找到最小的节点
     * @param
     */

    public E  min(){
        if(size == 0){
            throw  new IllegalArgumentException("is empty");

        }

        return mininum(root).e;
    }

    private Node mininum(Node node) {
        if(node.left ==null){
            return node;
        }

        return mininum(node.left);
    }

    /**
     * 找到最大值
     * @return
     */
    public E maxnium(){
        if(size == 0){
            throw  new IllegalArgumentException("is empty");

        }

        return maxnium(root).e;
    }

    private Node maxnium(Node node) {
        if(node.right ==null){
            return node;
        }
        //每次都是node.right 变成新的node
        return mininum(node.right);
    }
    //在二分搜索树种删除最小的
    public E removeMin(){
        E res = min();
        root = removeMin(root);
        return res;
    }

    //真正要删除最小的节点
    private Node removeMin(Node node) {
        if(node.left == null){
            //先把right 保存起来
            Node right = node.right;
            node.right =null;
            size--;
            return right;
        }
//        原来的feft = right
        node.left = removeMin(node.left);
        return node;
    }

    /**
     * 删除最大的值...
     * @return
     */
    public E removeMax(){
        E res = maxnium();
        root = removeMax(root);
        return res;
    }

    /***
     * 删除以node为根的最大节点
     * 返回根....
     * @param node
     * @return
     */
    private Node removeMax(Node node) {
        if(node.right == null){
            Node left  = node.left;
            node.left =null;
            size--;
            return left;
        }
        //原来的right节点
        node.right = removeMax(node.right);
        return  node;
    }
    //重二分搜索树中删除元素为E的节点
    public void remove(E  e ){

      root=   remove(root,e);
    }

    //删除node为根 e的元素
    private Node remove(Node node, E e) {
        if(node == null){
            return null;
        }
        //找left
        if(e.compareTo(node.e) < 0){
           node.left =  remove(node.left,e);
           return node;
            //去right 那边找
        }else if(e.compareTo(node.e) > 0){
            node.right =  remove(node.right,e);
            return node;
        }else  {//(e.compareTo(node.e) == 0)

            //如果左子树为空
            if(node.left == null){
                Node right = node.right;
                node.right =null;
                size --;
                return right;
            }

            if(node.right == null){
                Node left = node.left;
                node.left = null;
                size--;
                return left;

            }

            //如果左右子树都不为空..
            //找到比待删除节点大的最小节点,就是->删除节点右子树的最小节点
            //用这个节点替代删除节点的位置
            //1.找到右子树的最小节点
            Node successor = mininum(node.right);
            //将新的节点右子树= 删除最小节点后的
            successor.right =removeMin(node.right);
            //原来的左子树指向新的
            successor.left = node.left;

            //删除
            node.left = node.right = null;
            return successor;

        }

    }

    public static void main(String[] args) {
        BST<Integer> bst = new BST<>();

        int[] nums = {30,25,21,26,40,45,50};
        for (int num : nums) {
            bst.add(num);

        }
    }
}
